#!/bin/bash

set -o errexit
set -o pipefail

export LC_ALL=C

AN_DIR=$1

#for AN_DIR in `find $BASE_AN_DIR -maxdepth 1 -mindepth 1 -type d -mmin +360 | sort`; do

  REMOTE_HOST=$(cat ${AN_DIR}/worker_hostname)

  CNT_REMAINING=$(ls ${AN_DIR}/src_faa | wc -l)
  if [ $CNT_REMAINING -eq 0 ] ; then
    continue
  fi

  echo "Annotate $AN_DIR: $CNT_REMAINING at $REMOTE_HOST" >> /dev/stderr

  if [[ $REMOTE_HOST == "quad"* ]] ; then
    ANN_SCRIPT="6_annotation/annotate_wu_nw_simple_local.sh"
  else
    if [[ $REMOTE_HOST == "fs"* ]] ; then
      ANN_SCRIPT="6_annotation/annotate_wu_nw_simple_local_fs.sh"
    else
      echo "Unknown host type: $REMOTE_HOST" >> /dev/stderr
      continue
    fi
  fi

  ssh ${REMOTE_HOST} 'cd ~/trunk/gene-class && find '"$AN_DIR"'/src_faa/ -type f -name "*.faa" | sort | xargs -n1 -P1 -I {} bash -c "cat {} | '"${ANN_SCRIPT}"' '"${AN_DIR}"' && rm {}"'

  6_annotation/clear_bad_ann_dirs.py $AN_DIR
  9_annotation_analysis/create_results_exact_go.sh $AN_DIR

#done

